//
//  MessageTotalViewController.m
//  EDPS
//
//  Created by dllo on 15/11/30.
//  Copyright © 2015年 张维扬. All rights reserved.
//

#import "MessageTotalViewController.h"
#import "MessageViewController.h"
#import "DetailMessageViewController.h"
#import "Height.h"
#import "CoreDataManager.h"
#import "SaveText.h"

@interface MessageTotalViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain) UITableView *tableView;
@property (nonatomic,retain)NSMutableArray *rsesultArr;
@property (nonatomic,retain)CoreDataManager *manager;

@end

@implementation MessageTotalViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
    self.manager = [CoreDataManager ShareInstanceCoreDataManager];
    [self creatData];
    [self creatViews];
}
- (void)creatData
{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"SaveText" inManagedObjectContext:self.manager.managedObjectContext];
    [fetchRequest setEntity:entity];
    NSError *error = nil;
    NSArray *fetchedObjects = [self.manager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    self.rsesultArr = [NSMutableArray arrayWithArray:fetchedObjects];
}
- (void)creatViews
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle =  UITableViewCellSeparatorStyleNone;
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor redColor];
    [self.view addSubview:self.tableView];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 50)];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake((ScreenWidth - 150)/2, 10, 150, 30);
    [backButton setTitle:@"写游记" forState:UIControlStateNormal];
    [backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(Write) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:backButton];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.rsesultArr.count;
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    //调用父类的方法
    [super setEditing:editing animated:animated];
    //利用系统编辑按钮 控制tableView编辑状态
    [self.tableView setEditing:editing animated:animated];
    
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除游记";
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        SaveText *SaveText = [self.rsesultArr objectAtIndex:indexPath.row];
        [self.manager.managedObjectContext  deleteObject:SaveText];
        [self.rsesultArr removeObjectAtIndex:indexPath.row];
        NSArray *arr = @[indexPath];
        [self.tableView deleteRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationRight];
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"deq"];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"deq"];
    }
    SaveText *SaveText = [self.rsesultArr objectAtIndex:indexPath.row];
    NSString *textString = SaveText.text;
    cell.textLabel.text = textString;
    cell.textLabel.numberOfLines = 0;
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SaveText *SaveText = [self.rsesultArr objectAtIndex:indexPath.row];
    DetailMessageViewController *detail = [[DetailMessageViewController alloc] init];
    detail.saveText = SaveText;
    [self.navigationController pushViewController:detail animated:YES];
}
- (void)back
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)Write
{
    MessageViewController *message = [[MessageViewController alloc] init];
    [self.navigationController pushViewController:message animated:YES];
    [self.tableView reloadData];
}

@end